//
//  ViewController.swift
//  SelectImage
//
//  Created by teleframe on 15/8/12.
//  Copyright (c) 2015年 tf. All rights reserved.
//

import UIKit

class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate{

    @IBOutlet var selectedImageView: UIImageView!
    var alertCtrl : UIAlertController!
    
    //sddfsjaskj
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        setUpAlertCtrl()
    }

    //
    @IBAction func selectImage(sender: AnyObject) {
        
        self.presentViewController(self.alertCtrl, animated: true, completion: nil)
    }
    
    
    func setUpAlertCtrl(){
      
        self.alertCtrl = UIAlertController(title: "select Image", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
     //Create an action
        
        var carema : UIAlertAction = UIAlertAction(title: "Carame", style: UIAlertActionStyle.Default) { (action : UIAlertAction!) -> Void in
          self.carema()
        }
        
        let imageGallery :UIAlertAction = UIAlertAction(title: "Image Gallery", style: UIAlertActionStyle.Default) { (action : UIAlertAction!) -> Void in
            self.imageGallery()
        }
        
        let cancel : UIAlertAction = UIAlertAction(title:"Cancel", style: UIAlertActionStyle.Cancel) { (
            action:UIAlertAction!) -> Void in
            
            self.dismissViewControllerAnimated(true, completion: nil)
        }
        
        self.alertCtrl.addAction(carema)
        self.alertCtrl.addAction(imageGallery)
        self.alertCtrl.addAction(cancel)
    }

    
    func carema(){
    
        let picker = UIImagePickerController()
        picker.delegate = self
        picker.sourceType = .Camera
        self.presentViewController(picker, animated: true, completion: nil)
    }
    
    func imageGallery(){
        let picker = UIImagePickerController()
        picker.delegate = self
        picker.sourceType = .PhotoLibrary
        self.presentViewController(picker, animated: true, completion: nil)
    }
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
        selectedImageView.image = image
        self.dismissViewControllerAnimated(true, completion: nil)
    }

}

